// 云函数入口文件
const cloud = require('wx-server-sdk')
const env = "cloud1-7g4tme3pc72739f4"

cloud.init()

const db = cloud.database({env})
// 云函数入口函数
exports.main = async (event, context) => {
  // event包含前端传过来的参数和从微信获取到的用户信息

  const userInfo = event.userInfo

  // 连通数据库
  return await db.collection('group').add({
    data: {
      name: event.groupName,
      createBy: userInfo.openId,
      createTime: new Date(),
      delete: false,
      updateTime: new Date()
    }
  }).then(res=>{
    return db.collection('user-group').add({
      data: {
        groupId:res._id,
        userId: userInfo.userId,
        invalid: false
      }
    })
  })

}